﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using HRWEB.HtmlHelpers;

namespace HRWEB.Controllers
{
    [Authorize]
    public class RelationController : Controller
    {
        hrdbEntities ent = new hrdbEntities();
        
        public ActionResult Index()
        {
            string id = HttpContext.Profile["ID"].ToString();
            var scale = from hh in ent.THouseHolds where hh.FEmployeeID == id orderby hh.FBirthday descending select hh;
            return PartialView(scale.ToList());
        }
        public ActionResult Delete(int serialNO)
        {
            string id = HttpContext.Profile["ID"].ToString();
            var scale = from hh in ent.THouseHolds where hh.FEmployeeID == id orderby hh.FBirthday descending select hh;
            ent.THouseHolds.Remove(scale.Where(p=>p.FSerialNo==serialNO).Single());
            ent.SaveChanges();
            return Redirect(UrlHelper.GenerateUrl(null, "Index", "Home", null, null, "relation", new RouteValueDictionary(), Url.RouteCollection, Url.RequestContext, false));
        }
        public ActionResult Create()
        {
            return PartialView(ent.THouseHolds.Create());
        }
        [HttpPost]
        public ActionResult Create(THouseHold hh)
        {
            if (!hh.Validate()) {
                ModelState.AddModelError("Household", "社会关系填写不完整");
            }
            if (ModelState.IsValid)
            {
                string id = HttpContext.Profile["ID"].ToString();
                hh.FEmployeeID = id;
                ent.THouseHolds.Add(hh);
                ent.SaveChanges();
            }
            return Redirect(UrlHelper.GenerateUrl(null, "Index", "Home", null, null, "relation", new RouteValueDictionary(), Url.RouteCollection, Url.RequestContext, false));
        }
    }
}
